<?php

class NewsService {
	
	private static $instance = null;
	
	/**
	 * Class constructor 
	 */
	private function __construct() {
	
	}
	
	public static function getInstance(){
		self::$instance = self::$instance ? self::$instance : new NewsService();
		return self::$instance;		
	}
	
	public function getTable(){
		//TODO: Temporary backward competibility method. Requires reimplementation!!!
		$settings = &getService('GlobalSettings');	
	
		$content = "<div class=\"content\">\r\n";
		$content = $content . "<span class=\"marquee\"><marquee direction=\"left\">" . getLastPosts(10) . "</marquee></span>";
		
		$db = DBHandler::getInstance();
		$data = $db->executeSELECTQuery('news', null, null, 'date', '0, ' . $settings->get('news_per_page'));

		$yandexDirect = new YandexDirect();
		$yandexDirect->setTemplate(ADVERTISEMENT_TEMPLATES_PATH . 'YandexDirect.html');
		$content .= $yandexDirect->getHTML();

		// news list start
		$content .= "\r<div class=\"news\">";
		for ($i = 0; $i < count($data); $i++){
			$content .= "\r<div class=\"newsblock\">\r\n";
			$content .= '<b>' . $data[$i]['date'] . '</b>&nbsp;';
			$content .= '<b><a href="' . CONTEXT_PATH . 'index.php?action=news&amp;id=';
			$content .= $data[$i]['id'] . '" title="' . $data[$i]['caption'] . '">';
			$content .= $data[$i]['caption'] . "</a></b><br/>\r";
			$content .= '<img src="' . CONTEXT_PATH . 'news/news_' . $data[$i]['id'] . '.jpg" align="left" ';
			$content .= "alt=\"" . $data[$i]['caption'] . "\" width=\"62\" height=\"60\" />";
			$content .= $this->createHeader($data[$i]['text']) . "\r</div>";			
		}
		$content .= "\r</div>";
		// news list end
		
		// Begun 120x600 ads start
		$google120x600 = new BaseComponent();
		$google120x600->setTemplate(ADVERTISEMENT_TEMPLATES_PATH . 'Google-120x600.html');
		$content .= $google120x600->getHTML();
		// Begun 120x600 ads end

		$googleAddSense = new GoogleAddSense();
		$googleAddSense->setTemplate(ADVERTISEMENT_TEMPLATES_PATH . 'GoogleAddSense.html');
		$content .= $googleAddSense->getHTML();

		$content .= "</div>";
		return $content;		
	}
	
	function createHeader($text) {
		//TODO: Temporary backward competibility method. Requires reimplementation!!!		
		if (strlen($text) >= 255) {
			$i = 255;
			while ($text[$i] != ' ') {
				$i = $i -1;
			}
			if ($i > 0) {
				$text = substr($text, 0, $i) . "...";
			}
		}
		return $text;
	}		
	
	
	function getNewsText($id){
		//TODO: Temporary backward competibility method. Requires reimplementation!!!
				
		$db = DBHandler::getInstance();
		$data = $db->executeSELECTQuery('news', null, 'id = ' . $id);

		$content = "<div class=\"content\">\r\n";
		$content = $content . "<div class=\"newstext\">";
		$content = $content . '<img src="' . CONTEXT_PATH . 'news/news_' . $id . '.jpg" align="left" ';
		$content = $content . "alt=\"" . $data[0]["caption"] . "\">";
		$content = $content . "<h2>\r" . $data[0]["caption"] . "\r</h2>\r";
		$content = $content . $data[0]["text"] . "<br/><br/><br/>";
		$content = $content . $data[0]["referrer"];
		$content = $content . "</div>";
		$content = $content . "</div>";
		
		return $content;
	}
	
}

?>